﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspire.Entities
{
    public partial class CustomInt32Field : CustomField
    {
        /// <summary>
        /// Returns the Int32 value of this field as an object.
        /// </summary>
        /// <returns>(object) The Int32 value of this field.</returns>
        public override object GetData()
        {
            return this.Data;
        }

        /// <summary>
        /// Implementation of the abstract SetData method for the CustomInt32Field class.
        /// </summary>
        /// <param name="data"></param>
        public override void SetData(Object data)
        {
            // If we're passed an int
            if (data is Int32)
            {
                this.Data = (Int32)data;
                return;
            }

            // If we're passed a string
            if (data is String)
            {
                try
                {
                    this.Data = Int32.Parse(data.ToString());
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e.Message);
                    Console.Out.WriteLine(e.StackTrace);
                }
                return;
            }

            // If nothing else works
            this.Data = 0;
        }

        /// <summary>
        /// Returns the Int32 value of this field as a string.
        /// </summary>
        /// <returns>(string) The Int32 value of this field as a string or "Null" if the value is null.</returns>
        public override string ToString()
        {
            if (this.Data != null)
            {
                return this.Data.ToString();
            }

            return "Null";
        }
    }
}
